package cn.jasonone.jasonfx.interceptor;

import java.lang.reflect.Field;
import java.util.Objects;

import cn.jasonone.jasonfx.entity.JFXBaseController;
import cn.jasonone.jasonfx.entity.Optional;
import cn.jasonone.jasonfx.util.ClassUtil;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 文本框拦截器
 * 
 * @author Jason
 * @date: 2020-01-25 14:46
 */
@Slf4j
public class TextFieldInterceptor extends PropertyInterceptor<TextField> {

	public TextFieldInterceptor(TextField node, Object source, Field field) {
		super(node, source, field);
		node.textProperty().addListener((o, old, n) -> {
			log.trace("控件[{}(#{})]值变更,更新控件[{}]值为:{}",getNode().getClass().getSimpleName(),getNode().getId(),getField().getName(),n);
			ClassUtil.set(field, source, n);
		});
	}

	@Override
	public void intercept(Object obj, MethodProxy proxy, Object[] args, Optional<Object> returnValue,
			InterceptorChain chain) throws Throwable {
		log.trace("字段[{}]值变更,更新控件[{}(#{})]值为:{}",getField().getName(),getNode().getClass().getSimpleName(),getNode().getId(),args[0]);
		getNode().setText(Objects.toString(args[0], ""));
	}

}
